package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.ThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial>
        implements WmMaterialService {

    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private String beseUrl;
    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        if (file == null) return errorParam;

        //2.上传图片到fasfdfs,并返回一个地址
        String s = null;
        try {
            s = fastDFSClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3.保存素材数据到表中 wm_material
        WmMaterial material = new WmMaterial();
        material.setUserId(ThreadLocalUtils.getUserId());
        material.setUrl(s);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        save(material);
        return ResponseResult.okResult(beseUrl + s);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
//        if (dto==null)return errorParam;
        LambdaQueryWrapper<WmMaterial> qw = new LambdaQueryWrapper<>();
        qw.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
        qw.eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());
        qw.orderByDesc(WmMaterial::getCreatedTime);
        ResponseResult PageDate = getPageData(dto, qw);
        List<WmMaterial> list = (List<WmMaterial>) PageDate.getData();
        list.forEach(v->{v.setUrl(beseUrl+v.getUrl());});
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if (id==null)return errorParam;
        //判断素材表是否存在
        WmMaterial material = getById(id);
        if (material==null)return errorParam;
        //查询中间表是否有数据
        Integer count = wmNewsMaterialMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if (count>0)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"数据被引用");
        //先删除资源服务器的图片，在删除表中的图片
        fastDFSClient.delFile(material.getUrl());
        removeById(id);
        return success;
    }

    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        if (id == null)return errorParam;
        if (getById(id)==null)return errorParam;
        //修改状态
        update(Wrappers.<WmMaterial>lambdaUpdate().set(type!=null,WmMaterial::getIsCollection,type).eq(WmMaterial::getId,id));
        return success;
    }
}
